var lineReader = require('line-reader');
var fileSave = require('file-save');
var _ = require("underscore");
var of = null;
var oparam = {};
var ojavacode = [];

lineReader.eachLine('config.json', function(line, last) {
    oparam = {};
    ojavacode = [];
    
    var val = JSON.parse(line);

    Serialize(val.primary, val[val.primary], "", 0);

    of = fileSave("com/dunchong/entity/" + val.entity + ".java");

    write("package com.dunchong.entity;");
    write();
    write("import javax.servlet.http.HttpServletRequest;");
    write();
    write("import com.alibaba.fastjson.JSON;");
    write("import com.alibaba.fastjson.JSONArray;");
    write("import com.alibaba.fastjson.JSONObject;");
    write("import com.dunchong.util.GlobalUtil;");
    write();
    write("public class " + val.entity + " {");

    for (var param in oparam)
        write("private " + oparam[param] + " " + param + ";", 1);

    write();
    write("JSONArray jsonArrOpts;", 1);

    write();
    write("public boolean jsonIsEmpty() {", 1);
    write("return jsonArrOpts == null || jsonArrOpts.isEmpty();", 2);
    write("}", 1);

    write("    public int getOptionCnt() {");
    write("        return jsonArrOpts.size();");
    write("    }");
    write("    ");
    write("    public static " + val.entity + " parse(HttpServletRequest request) {");
    write("        " + val.entity + " " + val.obj + " = new " + val.entity + "();");
    write("        GlobalUtil.o(\"----- \" +  request.getParameter(\"" + val.primary + "\"));");
    write("        " + val.obj + ".jsonArrOpts = JSON.parseArray(request.getParameter(\"" + val.primary + "\"));");
    write("        return " + val.obj + ";");
    write("    }");
    write();
    write("    public void jsonReduce() {");

    for (var i = 0; i < ojavacode.length; i++)
        write(ojavacode[i], 2);
    write("        jsonArrOpts.remove(0);");
    write("    }");
    write("}");
});

function write(str, inum) {
    if (inum != null && _.isNumber(inum))
        of.write(print(inum) + str + "\n");
    else if (str != null)
        of.write(str + "\n");
    else
        of.write("\n");
}

function print(inum) {
    var str = "";
    for (var i = 0; i < inum; i++)
        str += "    ";
    return str;
}

function Serialize(skey, v, spref, index) {
    if (_.isArray(v))
    {
        oparam[spref + "_" + skey] = "String[]";
        ojavacode.push(print(index) + spref + "Json.getJSONArray(\"" + skey + "\").toArray(" + spref + "_" + skey + ");");
    }
    else if (_.isObject(v))
    {
        if (index == 0)
            ojavacode.push(print(index) + "JSONObject " + skey + "Json = jsonArrOpts.getJSONObject(0);");
        else
            ojavacode.push(print(index) + "JSONObject " + skey + "Json = " + spref + "Json.getJSONObject(\"" + skey + "\");");
        ojavacode.push(print(index) + "if (" + skey + "Json != null)");

        ojavacode.push(print(index) + "{");

        for (var param in v)
        {
            Serialize(param, v[param], skey, index + 1);
        }
        ojavacode.push(print(index) + "}");
    }
    else
    {
        oparam[spref + "_" + skey] = v;
        ojavacode.push(print(index) + spref + "_" + skey + " = " + spref + "Json.get" + v + "(\"" + skey + "\");");
    }
}